package com.wsoft.bpm.controller;

import com.wsoft.bpm.dto.RunProcessDTO;
import com.wsoft.bpm.service.RunProcessService;
import com.wsoft.bpm.vo.ApprovalNodeDataVO;
import com.wsoft.response.ResponseResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.task.Task;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author zhonglj
 * @date 2024-09-19 16:05
 **/
@RestController
@Tag(name = "工作流运行时管理")
@RequestMapping("/bpm/run")
public class RunProcessController {

    @Resource
    private RunProcessService runProcessService;

    @Operation(summary = "流程启动", description = "流程启动")
    @PostMapping("/startProcess")
    public ResponseResult startProcess(@RequestBody RunProcessDTO dto) {
        runProcessService.startProcess(dto.getProcessDefinitionKey(), dto.getTableName(), dto.getBusinessKey(), dto.getMap(), dto.getUserId());
        return ResponseResult.success("[sys.msg.success]");
    }

    @Operation(summary = "流程同意", description = "流程同意")
    @GetMapping("/agree")
    public ResponseResult agree(@Parameter(description = "任务id") String taskId,
                                @Parameter(description = "意见") String comment) {
        runProcessService.agree(taskId, comment);
        return ResponseResult.success("[sys.msg.success]");
    }

    @Operation(summary = "设置节点负责人", description = "设置节点负责人")
    @GetMapping("/setAssign")
    public ResponseResult setAssign(@Parameter(description = "任务id") String taskId,
                                    @Parameter(description = "负责人") String assignee) {
        runProcessService.setAssign(taskId, assignee);
        return ResponseResult.success("[sys.msg.success]");
    }

    @Operation(summary = "退回到上一个人", description = "退回到上一个人")
    @GetMapping("/backOne")
    public ResponseResult backOne(@Parameter(description = "任务id") String taskId,
                                  @Parameter(description = "意见") String comment) {
        runProcessService.backOne(taskId, comment);
        return ResponseResult.success("[sys.msg.success]");
    }

    @Operation(summary = "退回到提交人", description = "退回到提交人")
    @GetMapping("/back")
    public ResponseResult rollback(@Parameter(description = "流程实例id") String processInstanceId,
                                   @Parameter(description = "意见") String comment) {
        runProcessService.rollback(processInstanceId, comment);
        return ResponseResult.success("[sys.msg.success]");
    }

    @Operation(summary = "分发", description = "分发")
    @GetMapping("/handout")
    public ResponseResult handout(@Parameter(description = "任务id") String taskId,
                                  @Parameter(description = "分发负责人") String nextAssignee) {
        return ResponseResult.success("[sys.msg.success]");
    }

    @Operation(summary = "待办列表", description = "待办列表")
    @GetMapping("/todoList")
    public ResponseResult<List<Task>> todoList() {
        return ResponseResult.success(runProcessService.todoList());
    }

    @Operation(summary = "已办列表", description = "已办列表")
    @GetMapping("/doneList")
    public ResponseResult<List<HistoricTaskInstance>> doneList() {
        return ResponseResult.success(runProcessService.doneList());
    }

    @Operation(summary = "审批历史", description = "审批历史")
    @GetMapping("/history")
    public ResponseResult<List<Map>> history(@Parameter(description = "流程实例id") String processInstanceId) {
        return ResponseResult.success(runProcessService.history(processInstanceId));
    }

    @Operation(summary = "流程加签", description = "流程加签")
    @GetMapping("/addCandidate")
    public ResponseResult addCandidate(@Parameter(description = "任务id") String taskId,
                                       @Parameter(description = "加签人id") String candidateUserId) {
        return ResponseResult.success("[sys.msg.success]");
    }

    @Operation(summary = "结束流程", description = "结束流程")
    @GetMapping("/stopProcess")
    public ResponseResult stopProcess(@Parameter(description = "流程实例id") String processInstanceId) {
        runProcessService.stopProcess(processInstanceId);
        return ResponseResult.success("[sys.msg.success]");
    }


    @Operation(summary = "获取审批节点数据", description = "获取审批节点数据")
    @GetMapping("/getNodeData")
    public ResponseResult<ApprovalNodeDataVO> getNodeData(@Parameter(description = "业务唯一值") String businessKey,
                                                          @Parameter(description = "任务id") String taskId) {
        return ResponseResult.success(runProcessService.getNodeData(businessKey, taskId));
    }
}
